<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: $
 */
?>
<article class="bean addressbook">	
	<hgroup>
<?php
$users = $bean->ownUser;
// person or company?
if ($bean->person->company):
	// company
?>
		<h1><?php echo htmlspecialchars($bean->person->organization); ?></h1>
		<h2><?php echo htmlspecialchars(implode(' ', array($bean->person->title, $bean->person->first_name, $bean->person->last_name, $bean->person->suffix))); ?></h2>
		<h3><?php echo htmlspecialchars($bean->person->job_title); ?></h3>
<?php
else:
	// person
?>
		<h1><?php echo htmlspecialchars(implode(' ', array($bean->person->title, $bean->person->first_name, $bean->person->last_name, $bean->person->suffix))); ?></h1>
		<h2><?php echo htmlspecialchars($bean->person->organization); ?></h2>
		<h3><?php echo htmlspecialchars($bean->person->job_title); ?></h3>
<?php
endif;
?>
		<figure>
			<img src="<?php echo $bean->gravatar(); ?>" alt="<?php echo htmlspecialchars($bean->person->name); ?>" class="avatar" />
		</figure>
	</hgroup>
	
	<section class="list">
		<h2><?php echo __('names'); ?></h2>
		<div class="fn label"><?php echo __('person_nickname_label'); ?></div>
		<div class="fn value"><?php echo htmlspecialchars($bean->person->nickname); ?></div>
	</section>

	<section class="list">
		<h2><?php echo __('phones'); ?></h2>
<?php
foreach ($bean->ownPhone as $id=>$phone):
?>
			<div class="fn label"><?php echo __('phone_label_option_'.$phone->label); ?></div>
			<div class="fn value">
				<a
					href="tel:<?php echo htmlspecialchars($phone->value); ?>">
					<?php echo htmlspecialchars($phone->value); ?>
				</a>
			</div>
<?php
endforeach;
?>
	</section>
	
	<section class="list">
		<h2><?php echo __('emails'); ?></h2>
<?php
foreach ($bean->ownEmail as $id=>$email):
?>
			<div class="fn label"><?php echo __('email_label_option_'.$email->label); ?></div>
			<div class="fn value">
				<a
					href="mailto:<?php echo htmlspecialchars($email->value); ?>">
					<?php echo htmlspecialchars($email->value),"\n"; ?>
				</a>
			</div>
<?php
endforeach;
?>
	</section>
	
	<section class="list">
		<h2><?php echo __('urls'); ?></h2>
<?php
/**
 * display urls
 */
$pat = array('http://', 'https://');
$rep = array('', '');
// check if there is a personal home page
if ($bean->person->home_page):
	$value = str_replace($pat, $rep, $bean->person->home_page);
?>
			<div class="fn label"><?php echo __('url_label_option_personal'); ?></div>
			<div class="fn value">
				<a
					href="<?php echo htmlspecialchars($bean->person->home_page); ?>">
					<?php echo htmlspecialchars($value)."\n"; ?>
				</a>
			</div>
<?php
endif;
// for all urls related to this addressbook entry
foreach ($bean->ownUrl as $id=>$url):
	$value = str_replace($pat, $rep, $url->value);
?>
			<div class="fn label"><?php echo __('url_label_option_'.$url->label); ?></div>
			<div class="fn value">
				<a
					href="<?php echo htmlspecialchars($url->value); ?>">
					<?php echo htmlspecialchars($value)."\n"; ?>
				</a>
			</div>
<?php
endforeach;
?>
	</section>
	
<?php
if ($birth_date = $bean->birth_date()):
?>
	<section class="list">
		<div class="fn label"><?php echo __('person_birth_date_label'); ?></div>
		<div class="fn value birth_date"><?php echo htmlspecialchars($birth_date); ?></div>
	</section>
<?php
endif;
?>

	<section class="list">
		<h2><?php echo __('related_persons'); ?></h2>
<?php
$urlPatternRelatedPerson = '%s/editor/view/addressbook/%s/';
foreach ($bean->ownRelated_person as $id=>$person):
	$addr = array_shift($person->person->ownAddressbook);
	$url = sprintf($urlPatternRelatedPerson, $language, $addr->getId());
?>
			<div class="fn label"><?php echo __('related_person_label_option_'.$person->label); ?></div>
			<div class="fn value">
				<a
					href="<?php echo $url; ?>">
					<?php echo htmlspecialchars($person->person->name); ?>
				</a>
			</div>
<?php
endforeach;
?>
	</section>
	
	<section class="list">
		<h2><?php echo __('addresses'); ?></h2>
<?php
foreach ($bean->ownAddress as $id=>$addr):
?>
			<div class="fn label"><?php echo __('address_label_option_'.$addr->label); ?></div>
			<div class="fn value"><?php echo nl2br(htmlspecialchars($addr->formatted_address)); ?></div>
<?php
endforeach;
?>
	</section>
	
	<section class="list">
		<div class="fn label"><?php echo __('person_note_label'); ?></div>
		<div class="fn value note"><?php echo textile($bean->person->note); ?></div>
	</section>
	
<?php
/**
 * If this addressbook entry is connected to a user account display so
 */
if (count($users)):
?>
	<section class="list">
		<h2><?php echo __('user'); ?></h2>
		<div class="fn label user"><?php echo __('user'); ?></div>
		<div class="fn value user">
<?php
	foreach ($users as $id=>$userAccount):
?>
			<a
				href="{language}/user/profile/<?php echo urlencode($userAccount->getId()); ?>">
				<?php echo $userAccount->view(); ?>
			</a>&nbsp;
<?php
	endforeach;
?>
		</div>
	</section>
<?php
endif;
?>

</article>